<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


function password_md5($value, $salt = '')
{
    return md5(config('secure.prefix_password') . $value . $salt);
}


function plural($singular)
{
    $end_one = substr($singular, -1);
    $end_two = substr($singular, -1);
    $end_second = substr($singular, -2, 1);
    $del_end_one = substr($singular, 0, -1);
    $del_end_two = substr($singular, 0, -2);
    $consonant = 'bcdfghjklmnpqrstvwxyz'; // 辅音字母
    $zoetic = [
        'Negro',    // 黑人
        'hero',     // 英雄
        'patato',   // 土豆
        'tomato',   // 西红柿
    ];
    if ($end_one == 's' || $end_one == 'x' || $end_two == 'sh' || $end_two == 'ch') {
        $plural = $singular . 'es';
    } else if ($end_one == 'y' && (strpos($consonant, $end_second) !== false) ) {
        $plural = $del_end_one . 'ies';
    } else if ($end_one == 'o') {
        if (in_array($singular, $zoetic)) {
            $plural = $singular . 's';
        } else {
            $plural = $singular . 'es';
        }
    } else if ($end_one == 'f') {
        $plural = $del_end_one . 'ves';
    } else if ($end_two == 'fe') {
        $plural = $del_end_two . 'ves';
    } else {
        $plural = '超出计算范围';
    }

    return $plural;
}


function make_btn_color_class()
{
    $color_class = [
        'btn-ultraviolet-rays-3',
        'btn-beetle-bus-goes-jamba-juice-4',
        'btn-friends-and-foes-2',
        'btn-indian-night-5',
    ];
    $key = array_rand($color_class);
    return $color_class[$key];
}

function getip() {
    $unknown = 'unknown';
    if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown) ) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif ( isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown) ) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    /*
    处理多层代理的情况
    或者使用正则方式：$ip = preg_match("/[\d\.]{7,15}/", $ip, $matches) ? $matches[0] : $unknown;
    */
    if (false !== strpos($ip, ','))
        $ip = reset(explode(',', $ip));
    return $ip;
}
